#include<functional>
#include<vector>
#include<pthread.h>
#include<iostream>
using namespace std;

pthread_cond_t cond;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
vector<function<void()>> funcs;
void show()
{
    cout<<"hello show"<<endl;
}
void print()
{
    cout<<"hello print"<<endl;
}
void* waitCommand(void* arg)
{
    pthread_detach(pthread_self());
    cout<<"!!!!!!!111111"<<endl;
    while(true)
    {
        pthread_cond_wait(&cond,&mutex);
        for(auto& f:funcs)
        {
            f();
        }
    }
    cout<<"thread id: "<<pthread_self()<<" end... "<<endl;
    return nullptr;
}
int main()
{
    funcs.push_back(show);
    funcs.push_back(print);
    funcs.push_back([](){
        cout<<"你好，条件变量！"<<endl;
    });
    
    pthread_cond_init(&cond,nullptr);

    pthread_t t1,t2,t3;
    pthread_create(&t1,nullptr,waitCommand,nullptr);
    pthread_create(&t2,nullptr,waitCommand,nullptr);
    pthread_create(&t3,nullptr,waitCommand,nullptr);

    cout<<"2222222222222222"<<endl;
    while(true)
    {
        char n='a';
        cout<<"请输入你的command(a/q)：";
        cin>>n;
        if(n=='a')
        {
            pthread_cond_signal(&cond);
        }
        else
        {
            break;
        }
    }
    pthread_cond_destroy(&cond);
    return 0;
}